package cn.stylefeng.guns.modular.feedback.service.Impl;



import cn.hutool.core.bean.BeanUtil;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.enums.CommonStatusEnum;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.modular.User.enums.UserEnum;
import cn.stylefeng.guns.modular.feedback.entity.UserFeedback;
import cn.stylefeng.guns.modular.feedback.mapper.UserFeedbackMapper;
import cn.stylefeng.guns.modular.feedback.param.UserFeedbackParam;
import cn.stylefeng.guns.modular.feedback.service.UserFeedbackService;
import cn.stylefeng.guns.sys.modular.emp.service.SysEmpService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hankcs.hanlp.HanLP;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserFeedbackServiceImpl extends ServiceImpl<UserFeedbackMapper, UserFeedback> implements UserFeedbackService {

    @Resource
    private SysEmpService sysEmpService;

    @Resource
    private UserFeedbackMapper userFeedbackMapper;

    @Override
    public List<UserFeedback> list(UserFeedbackParam userFeedbackParam){
        LambdaQueryWrapper<UserFeedback> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(UserFeedback::getId, CommonStatusEnum.DELETED.getCode());
        return this.list(queryWrapper);
    }

    @Override
    public void add(UserFeedbackParam userFeedbackParam) {
        UserFeedback userFeedback = new UserFeedback();
        BeanUtil.copyProperties(userFeedbackParam,userFeedback);
        this.save(userFeedback);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(UserFeedbackParam userFeedbackParam) {
        UserFeedback userFeedback = this.queryCanteenUser(userFeedbackParam);

        this.updateById(userFeedback);
        Long id = userFeedback.getId();
        //删除该用户对应的员工表信息
        sysEmpService.deleteEmpInfoByUserId(id);

        this.removeById(id);
    }



    @Override
    public void edit(UserFeedbackParam userFeedbackParam) {
        UserFeedback userFeedback = this.queryCanteenUser(userFeedbackParam);
        BeanUtil.copyProperties(userFeedbackParam,userFeedback);
        this.updateById(userFeedback);
    }

    @Override
    public void extractKey(int pid,String opinion) {
        List<String> keywords = HanLP.extractKeyword(opinion,10);
        for(String keyword:keywords){
            userFeedbackMapper.updateKeyWords(pid,keyword);
        }

    }

    @Override
    public List<String> lookTopTen() {
        return userFeedbackMapper.selectTopKey();
    }

    private UserFeedback queryCanteenUser(UserFeedbackParam userFeedbackParam) {
        UserFeedback userFeedback = this.getById(userFeedbackParam.getId());
        if (ObjectUtil.isEmpty(userFeedback)) {
            throw new ServiceException(UserEnum.NOT_EXISTED);
        }
        return userFeedback;

    }

}
